package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class dateTest extends JFrame {

    JLabel jl1  = new JLabel();
    private final String DEFAULT_TIME_FORMAT = "HH:mm:ss\n MM月dd日,EEEE";
    private String time;
    private final int ONE_SECOND = 1000;
    public dateTest(){
        setLayout(new GridLayout(5,1));
        setSize(450,300);

        configTimeArea();
        setTitle("我的闹钟");
        setLocationRelativeTo(null);

        JLabel[] jl = new JLabel[2];
        jl[0] = new JLabel("当 前 时 间：");


        JPanel[] jp = new JPanel[3];
        jp[0] = new JPanel();
        jp[0].add(jl[0]);
        jp[0].add(jl1);
        add(jp[0]);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    //  设置time计时器，每隔1000毫秒就刷新一次
    private void configTimeArea() {
        java.util.Timer tmr = new Timer();
        tmr.scheduleAtFixedRate(new JLabelTimerTask(), new Date(), ONE_SECOND);
    }
    /**
     * Timer task 更新时间显示区
     *
     */
    protected  class JLabelTimerTask extends TimerTask {
        SimpleDateFormat dateFormatter = new SimpleDateFormat(
                DEFAULT_TIME_FORMAT);
        public void run() {
            time = dateFormatter.format(Calendar.getInstance().getTime());
            jl1.setText(time);
        }
    }
    public static void main(String[] args) {
        new dateTest();
    }
}

